﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.ServiceModel;
using BLL;
using Common;
using Model;

namespace PositionServer.Service
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, IncludeExceptionDetailInFaults = true)]
    class PositionService : IPositionService
    {
        public int TestWCF()
        {
            return 1;
        }


        readonly BDriver _bDriver = new BDriver();
        readonly BTaxi _bTaxi = new BTaxi();
        readonly BTrade _bTrade = new BTrade();
        readonly BUser _bUser = new BUser();
        readonly BTaxiGpsHistory _bTaxiGpsHistory = new BTaxiGpsHistory();
        readonly BRole _bRole = new BRole();
        readonly BComplaint _bComplaint = new BComplaint();
        readonly BTaxiADInfo _bTaxiAd = new BTaxiADInfo();
        readonly BTaxiGroup _bTaxiGroup = new BTaxiGroup();
        readonly BTaxiArea _bTaxiArea = new BTaxiArea();
        readonly BGmapoverlay _bGmapoverlay = new BGmapoverlay();
        readonly BPhoneuser _bPhoneuser = new BPhoneuser();
        readonly BCallRecords _bCallRecords = new BCallRecords();
        readonly BTaxiWarming _bTaxiWarming = new BTaxiWarming();
        readonly BServerConfig _bServerConfig = new BServerConfig();
        readonly BAdMachine _bAdMachine = new BAdMachine();
        readonly BPrivateCar _bPrivateCar = new BPrivateCar();
        private readonly BTaxiOnlineTime _bTaxiOnlineTime = new BTaxiOnlineTime();
        private readonly BTaxiRouteHistory _bTaxiRouteHistory = new BTaxiRouteHistory();
        private readonly BTaxiNoPsgDistanceConfig _btaxiNoPsgConfig = new BTaxiNoPsgDistanceConfig();
        private readonly BCompany _bCompany = new BCompany();
        private readonly BCityOfRentCar _bCityOfRentCar = new BCityOfRentCar();
        private readonly BRentCar _bRentCar = new BRentCar();
        private readonly BSpecialCar _bSpecialCar = new BSpecialCar();
        private readonly BPhonePlatform _bPhonePlatform = new BPhonePlatform();
        private readonly BCardCheckHistory _bCardCheckHistory = new BCardCheckHistory();
        private readonly BDestination _bDestination = new BDestination();
        private readonly BTaxiCDMA _bTaxiCdma = new BTaxiCDMA();


        public void UpdateTaxiInfo(TaxiInfo taxiInfo)
        {
          
            if (!PositionMainForm.TaxiList.Contains(taxiInfo.DriverPhoneNumber))
            {

                if (taxiInfo.CarType == 0)
                {
                    if (_bDriver.ExistsDriverPhoneNumber(taxiInfo.DriverPhoneNumber) != null)
                    {
                        TaxiRegister(taxiInfo.DriverPhoneNumber, taxiInfo.Ip);
                    }
                    else if (_bTaxiCdma.ExistsPhoneNumber(taxiInfo.DriverPhoneNumber))
                    {
                        CdmaTaxiRegister(taxiInfo);
                    }
                    else if (_bPrivateCar.Exists(taxiInfo.DriverPhoneNumber))
                    {
                        PrivateCarRegister(taxiInfo.DriverPhoneNumber, taxiInfo.Ip);
                    }
                    else if (_bSpecialCar.PhoneNumberExists(taxiInfo.DriverPhoneNumber))
                    {
                        SpecialCarRegister(taxiInfo.DriverPhoneNumber, taxiInfo.Ip);
                    }
                }
                else
                {
                    switch (taxiInfo.CarType)
                    {
                        case ConstParameters.CarTypeOfAmbulance:
                         SpecialCarRegister(taxiInfo.DriverPhoneNumber, taxiInfo.Ip);
                            break;
                  
                    }
                } 
            }

            TaxiInfo taxiInfoCache = PositionMainForm.TaxiList[taxiInfo.DriverPhoneNumber] as TaxiInfo;
            if (taxiInfoCache != null)
            {
                taxiInfoCache.GpsCount++;//******
                taxiInfoCache.DriverPhoneNumber = taxiInfo.DriverPhoneNumber;
                taxiInfoCache.UpdateTime = taxiInfo.UpdateTime;
                taxiInfoCache.TaxiState = taxiInfo.TaxiState;


                taxiInfoCache.Latitude = taxiInfo.Latitude;
                taxiInfoCache.Longitude = taxiInfo.Longitude;


                taxiInfoCache.Ip = taxiInfo.Ip;
                taxiInfoCache.AlarmL = taxiInfo.AlarmL;

                taxiInfoCache.Angle = taxiInfo.Angle;
                taxiInfoCache.Speed = taxiInfo.Speed;
            }
        }

        public void UpdateTaxiCallingState(string phoneNum, byte state)
        {
            TaxiInfo taxiInfoCache = PositionMainForm.TaxiList[phoneNum] as TaxiInfo;
            if (taxiInfoCache != null) taxiInfoCache.IsCalling = state;
        }

        public void AddTaxiGpsHistoryInfo(TaxiGpsHistoryInfo taxiGpsHistoryInfo)
        {
            _bTaxiGpsHistory.Add(taxiGpsHistoryInfo);
        }

        public DataSet TaxiGpsHistoryGetList(string driverPhoneNumber, DateTime startTime, DateTime endTime)
        {
            return _bTaxiGpsHistory.GetList(driverPhoneNumber, startTime, endTime);
        }
        public DataSet TaxiGpsHistoryGetListWhere(string strWhere)
        {
            return _bTaxiGpsHistory.GetList(strWhere);
        }

        public int TaxiGpsHistoryGetRecordCount(string strWhere)
        {
            return _bTaxiGpsHistory.GetRecordCount(strWhere);
        }

        public DataSet TaxiGpsHistoryGetStatisticsList(string strWhere)
        {
            return _bTaxiGpsHistory.GetStatisticsList(strWhere);
        }

        public DataSet GetTaxiOnlineCountReport(string strWhere)
        {
            return _bTaxiGpsHistory.GetTaxiOnlineCountReport(strWhere);
        }

        public DataSet TaxiGpsHistoryGetStatisResult(int intObject, string strValue,
           int dateType, string strWhere)
        {
            return _bTaxiGpsHistory.GetStatisResult(intObject, strValue, dateType, strWhere);
        }
        public bool TaxiRegister(string phoneNumber, IPAddress ip)
        {

            ArrayList al = _bDriver.ExistsDriverPhoneNumber(phoneNumber);

            if (al != null)
            {

                int taxiId = Convert.ToInt32(al[0]);
                string driverName = al[1].ToString();
                //--id-TaxiNo-TaxiType -Remark
                TaxiInfo taxiInfo = _bTaxi.GetModel(taxiId);
                if (taxiInfo != null)
                {
                    taxiInfo.DriverPhoneNumber = phoneNumber;
                    taxiInfo.DriverName = driverName;
                    taxiInfo.Ip = ip;
                    taxiInfo.IsCalling = 0;
                    taxiInfo.StartLevel = _bDriver.GetDriverStar(phoneNumber);
                    taxiInfo.CarType = 1; //出租车
                    
                    PositionMainForm.TaxiList[phoneNumber] = taxiInfo;
                    //LogService.WriteDebugLog(phoneNumber + "出租车添加到内存中。+++", DateTime.Now);

                    return true;
                }
            }
            return false;
 
        }
        public string TaxiRegister2(string phoneNumber, IPAddress ip, string IMEI, int version)
        {
            // taxiId、driverName、readIMEI、Serverid
            ArrayList al = _bDriver.ExistsDriverPhoneNumber(phoneNumber);

            if (al != null)
            {
                string readIMEI = al[2].ToString();
                if (string.IsNullOrEmpty(readIMEI))
                {
                    //set IMEI 到数据库
                    //更新到数据库
                    if(_bDriver.UpdateIMEIByPhoneNumber(phoneNumber, IMEI))
                    {
                        Console.WriteLine("IMEI更新成功！");
                    }
                     else
                    {
                        Console.WriteLine("IMEI更新失败！");
                    }                    
                }
                else
                {
                    if (readIMEI != IMEI && IMEI != "")
                    { return "1"; }//IMEI 验证失败
                }

                int taxiId = Convert.ToInt32(al[0]);
                string driverName = al[1].ToString();
                //--id-TaxiNo-TaxiType -Remark
                TaxiInfo taxiInfo = _bTaxi.GetModel(taxiId);
                if (taxiInfo != null)
                {
                    taxiInfo.DriverPhoneNumber = phoneNumber;
                    taxiInfo.DriverName = driverName;
                    taxiInfo.Ip = ip;
                    taxiInfo.IsCalling = 0;
                    taxiInfo.StartLevel = _bDriver.GetDriverStar(phoneNumber);
                    taxiInfo.Version = version;
                    taxiInfo.CarType = 1; //出租车
                    if (al[4] != null)
                    {
                        taxiInfo.Signature = al[4].ToString();
                    }
                    PositionMainForm.TaxiList[phoneNumber] = taxiInfo;

                    return "Serverid:" + al[3]+"|Signature:"+al[4];
                }
                else
                {
                    return "2";
                }
            }
            else
            {
                return "0"; //表示数据库没这个号码
            }

        }

        public bool PrivateCarRegister(string phoneNumber, IPAddress ip)
        {

          //  ArrayList al = _bDriver.ExistsDriverPhoneNumber(phoneNumber);
            PrivateCarInfo model = _bPrivateCar.GetModel(phoneNumber);
            if (model != null)
            {
                TaxiInfo car = new TaxiInfo();
                car.Ip = ip;
                car.DriverPhoneNumber = phoneNumber;
                car.CarType = 3;// 表示是私家车    
                car.Signature = model.Signature;
                car.TaxiNo = model.TaxiNo;
                car.TaxiType = model.TaxiType;
                car.DriverName = model.DriverName;
                
                PositionMainForm.TaxiList[phoneNumber] = car;
                //LogService.WriteDebugLog(phoneNumber + "出租车添加到内存中。+++", DateTime.Now);
                return true;
               
            }
            return false;

        }

        public string  PrivateCarRegister(string phoneNumber, IPAddress ip, string IMEI, int version)
        {
            PrivateCarInfo model = _bPrivateCar.GetModel(phoneNumber);
            if (model==null)
            {
                return "0";//数据库中无此号码
            }
            //if (model.IMEI!=IMEI)
            //{
            //    return "1";//IMEI不匹配
            //}
            TaxiInfo car = new TaxiInfo();
            car.Ip = ip;
            car.DriverPhoneNumber = phoneNumber;
            car.CarType = 3;// 表示是私家车                 
            car.Signature = model.Signature;
            car.Version = version;

            car.TaxiNo = model.TaxiNo;
            car.TaxiType = model.TaxiType;
            car.DriverName = model.DriverName;
            
            PositionMainForm.TaxiList[phoneNumber] = car;
            if (!_bPrivateCar.Update(phoneNumber, DateTime.Now))
            {
                Console.WriteLine("私家车登录信息更新失败..."+phoneNumber);
            }
            return "2|" + model.Signature;
        }


        public bool CdmaTaxiRegister(TaxiInfo taxiInfo)
        { 
            //  ArrayList al = _bDriver.ExistsDriverPhoneNumber(phoneNumber);
            TaxiCdmaInfo model = _bTaxiCdma.GetModelByPhoneNumber(taxiInfo.DriverPhoneNumber); 
            if (model != null)
            {
                TaxiInfo car = new TaxiInfo();
                car.Ip = taxiInfo.Ip;
                car.DriverPhoneNumber = taxiInfo.DriverPhoneNumber;
                car.CarType = 1; 
                car.TaxiNo = model.TaxiNo;
                car.TaxiType = model.TaxiType;
                car.CarDeviceType = taxiInfo.CarDeviceType;
                if (taxiInfo.DriverPhoneNumber==model.PhoneNumberDay)
                {
                    car.DriverName = model.DriverDay;
                }
                else
                {
                    car.DriverName = model.DriverNight;
                }                 
                car.CompanyName = model.CompanyName;
                car.Angle = taxiInfo.Angle;
                car.Speed = taxiInfo.Speed;
                car.UpdateTime = taxiInfo.UpdateTime;
                car.StartLevel = 5;
                PositionMainForm.TaxiList[taxiInfo.DriverPhoneNumber] = car;
                //LogService.WriteDebugLog(phoneNumber + "出租车添加到内存中。+++", DateTime.Now);
                return true;

            }
            return false;

        }

      

        public bool SpecialCarRegister(string phoneNumber, IPAddress ip)
        {

            //  ArrayList al = _bDriver.ExistsDriverPhoneNumber(phoneNumber);
            SpecialCarInfo model = _bSpecialCar.GetModelByPhoneNum(phoneNumber);
            if (model != null)
            {
                TaxiInfo car = new TaxiInfo();
                car.Ip = ip;
                car.DriverPhoneNumber = phoneNumber;
                car.CarType = model.Type;

                car.TaxiNo = model.CarNo;
                car.TaxiType = model.CarType;
                car.DriverName = model.DriverName;
                car.CompanyName = model.CompanyName;

                PositionMainForm.TaxiList[phoneNumber] = car;
                //LogService.WriteDebugLog(phoneNumber + "出租车添加到内存中。+++", DateTime.Now);
                return true;

            }
            return false;

        }
         

        public string SpecialCarRegister(string phoneNumber, 
            IPAddress ip, string imei, int version)
        {
            SpecialCarInfo model = _bSpecialCar.GetModelByPhoneNum(phoneNumber);
            if (model == null)
            {
                return "0";//数据库中无此号码
            }


            if (string.IsNullOrEmpty(model.Imei))
            {
                //set IMEI 到数据库
                //更新到数据库
                if (_bSpecialCar.UpdateImeiByPhoneNumber(phoneNumber,imei))
                {
                    Console.WriteLine("IMEI更新成功！");
                }
                else
                {
                    Console.WriteLine("IMEI更新失败！");
                }
            }
            else
            {
                if (model.Imei != imei)
                {
                    return "1";//IMEI不匹配
                }
            }

           
            TaxiInfo car = new TaxiInfo();
            car.Ip = ip;
            car.DriverPhoneNumber = phoneNumber;
            car.CarType = model.Type;
            car.Signature = model.Signature;
            car.TaxiNo = model.CarNo;
            car.TaxiType = model.CarType;
            car.DriverName = model.DriverName;
            car.ServerId = model.ServerId;
            car.CompanyName = model.CompanyName;

            PositionMainForm.TaxiList[phoneNumber] = car;
             
            return "2|" +model.Type+"|"+ 
                model.ServerId + "|" + model.Signature;
        }


        public PassengerInfo AddPassengerByClient(PassengerInfo passenger)
        {
            if (PositionMainForm.PassengerList.Contains(passenger.PhoneNumber))
            {
                PassengerInfo psg = PositionMainForm.PassengerList[
                    passenger.PhoneNumber] as PassengerInfo;
                psg.Lat = passenger.Lat;
                psg.Lng = passenger.Lng;
                psg.Type = passenger.Type;
                psg.Name = passenger.Name;
                psg.UpdateTime = passenger.UpdateTime;                
            }
            else
            {
                PositionMainForm.PassengerList[passenger.PhoneNumber] = passenger;
            }
           return PositionMainForm.PassengerList[passenger.PhoneNumber] as PassengerInfo;
        }

        public void DeletePassengerByClient(string phoneNumber)
        {
            PassengerInfo psgInfo = PositionMainForm.PassengerList[phoneNumber] as
                                    PassengerInfo;
            if (psgInfo!=null )
            {
                if (!string.IsNullOrEmpty(psgInfo.CarPhoneNumber))
                {
                    TaxiInfo taxInfo = PositionMainForm.TaxiList[psgInfo.CarPhoneNumber] as TaxiInfo;
                    if (taxInfo != null)
                    {
                        taxInfo.PassengerPhoneNumber = "";
                    }
                }                
                PositionMainForm.PassengerList.Remove(phoneNumber);
            }             
        }

        public void ClearCarPassengerByClient(string carPhone,string psgPhone)
        {
            TaxiInfo taxiInfo = PositionMainForm.TaxiList[carPhone] as TaxiInfo;
            if (taxiInfo != null)
            {
                taxiInfo.PassengerPhoneNumber = "";
                taxiInfo.DestinationId = 0;
            }          
            PassengerInfo psgInfo = PositionMainForm.PassengerList[psgPhone] as
                                    PassengerInfo;
            if (psgInfo != null)
            {
                psgInfo.CarPhoneNumber = "";
            }
        }
        
        public DestinationInfo AddDestinationByClient(DestinationInfo model)
        {
            //int id;
            //do
            //{
            //    id = int.Parse(DateTime.Now.ToString("HHmmssff"));
            //} 
            //while (PositionMainForm.DestinationList.Contains(id));
            //model.Id = id;
            model = DestinationAdd(model);
            PositionMainForm.DestinationList[model.Id] = model;
            return model;
        }

        public void DeleteDestinationById(int id)
        {
            if (DestinationDelete(id))
            {
                if (PositionMainForm.DestinationList.Contains(id))
                {
                    PositionMainForm.DestinationList.Remove(id);
                }    
            }             
        }
         
        public void ClearDestinationByClient()
        {
            PositionMainForm.DestinationList.Clear();
        }

        public List<DestinationInfo> GetNearbyDestinationInfo(
           double longitude, double latitude, double distance,string type)
        {
            double d = distance / 100.0f;
            double d2 = d;

            lock (PositionMainForm.DestinationList.SyncRoot)
            {
                List<DestinationInfo> destinationInfos = 
                    new List<DestinationInfo>();
                destinationInfos.AddRange(PositionMainForm.DestinationList
                    .Values.Cast<DestinationInfo>().
                     Where(destInfo => Math.Abs(destInfo.Lat - latitude) < d2
                       && Math.Abs(destInfo.Lng - longitude) < d
                       && type.Contains("|"+destInfo.Type+"|")
                       ));
                return destinationInfos;
            }
        }

        public List<DestinationInfo> GetAllDestinationInfo()
        {            
            lock (PositionMainForm.DestinationList.SyncRoot)
            {
                List<DestinationInfo> destinationInfos =
                    new List<DestinationInfo>();
                destinationInfos.AddRange(PositionMainForm.DestinationList
                .Values.Cast<DestinationInfo>());                  
                return destinationInfos;
            }
        }

        public int SetDestinationId(int type,string phoneNumber,int id)
        {
            switch (type)
            {
                    //车
                case 0:
                    TaxiInfo carInfo = PositionMainForm.TaxiList[phoneNumber] as TaxiInfo;
                    if (carInfo==null)
                    {
                        return 0;
                    }
                    carInfo.DestinationId = id;

                    lock (PositionMainForm.PassengerList.SyncRoot)
                    {
                        foreach (PassengerInfo psg in PositionMainForm.PassengerList.Values)
                        {
                            if (!string.IsNullOrEmpty(psg.CarPhoneNumber)
                                && psg.CarPhoneNumber==phoneNumber)
                            {
                                psg.DestinationId = id;
                            }
                        }   
                    }                     
                    return 1;      

                    //乘客
                case 1:
                    PassengerInfo psgInfo = PositionMainForm.PassengerList[
                        phoneNumber] as PassengerInfo;
                    if (psgInfo==null)
                    {
                        return 0;
                    }
                    psgInfo.DestinationId = id;
                    if (!string.IsNullOrEmpty(psgInfo.CarPhoneNumber))
                    {
                        TaxiInfo carInfo2 = PositionMainForm.TaxiList[
                            psgInfo.CarPhoneNumber] as TaxiInfo;
                        if (carInfo2 == null)
                        {
                            return 0;
                        }
                        carInfo2.DestinationId = id;  
                    }                      
                    return 1;
                default:
                    return 0;
            }
            
        }

        public int SetPassengerToCar(string carPhoneNum,
            byte carType,string psgPhoneNum)
        { 
            TaxiInfo carInfo = PositionMainForm.TaxiList[carPhoneNum] as TaxiInfo;
            PassengerInfo psgInfo = PositionMainForm.PassengerList[psgPhoneNum] as PassengerInfo;
            if (carInfo == null)
            {
                return 0;
            }
            if (psgInfo == null)
            {
                return 2;
            }
            if (carInfo.CarType!=carType)
            {
                return 3;
            }            
            carInfo.PassengerPhoneNumber = psgPhoneNum;
            psgInfo.CarPhoneNumber = carPhoneNum;
            psgInfo.CarNo = carInfo.TaxiNo;
            if (carInfo.DestinationId!=0)
            {
                psgInfo.DestinationId = carInfo.DestinationId;
            }
            else
            {
                if (psgInfo.DestinationId!=0)
                {
                    carInfo.DestinationId = psgInfo.DestinationId;
                }
            }
            return 1;
        }

        public int SetPassengerPresetValue(string psgPhoneNum,object presetValue)
        {
            PassengerInfo psgInfo = PositionMainForm.PassengerList[psgPhoneNum]
                as PassengerInfo;
            if (psgInfo==null)
            {
                return 0;
            }
            psgInfo.Preset = presetValue;
            return 1;
        }


        public bool UpdateCarSignature(string phoneNumber, string signature)
        {
            TaxiInfo car = PositionMainForm.TaxiList[phoneNumber] as TaxiInfo;
            if (car==null)
            {
                return false;
            }
            car.Signature = signature;
            return true;
        }

        public bool UpdatePrivateCarInfosInMemory(PrivateCarInfo model)
        {
            TaxiInfo car = PositionMainForm.TaxiList[model.PhoneNumber] as TaxiInfo;
            if (car == null)
            {
                return false;
            }
            car.TaxiNo = model.TaxiNo;
            car.TaxiType = model.TaxiType;
            car.DriverName = model.DriverName;
            
            return true;
        }

        public void PassengerUpdateInfo(string psgPhoneNum,
            double psgLat,double psgLng,IPAddress psgIp,byte carType)
        {
            PassengerInfo psg = PositionMainForm.PassengerList[psgPhoneNum]
                                as PassengerInfo;
            if (psg!=null)
            {
                psg.Lat = psgLat;
                psg.Lng = psgLng;
                psg.Ip = psgIp;
                psg.Type = carType;
                psg.UpdateTime = DateTime.Now;
            }
            else
            {
                psg = new PassengerInfo();
                psg.PhoneNumber = psgPhoneNum;
                psg.Lat = psgLat;
                psg.Lng = psgLng;
                psg.Ip = psgIp;
                psg.Type = carType;
                psg.Name = "";
                psg.UpdateTime = DateTime.Now;
                PositionMainForm.PassengerList[psgPhoneNum] = psg;
            }             
        }



        public void AsTcpRegister(string webip)
        {
            ArrayList al = new ArrayList();
            PositionMainForm.AcceptServrtIpList[webip] = al;
            //  UpdateRichBox();
        }

        public void AsTcpCountAdd(string webip, string phoneNumber)
        {
            if (PositionMainForm.AcceptServrtIpList.Contains(webip))
            {
                ArrayList al = PositionMainForm.AcceptServrtIpList[webip] as ArrayList;
                if (al != null)
                {
                    if (!al.Contains(phoneNumber))
                    {
                        al.Add(phoneNumber);
                    }
                }
            }
            else
            {
                ArrayList al = new ArrayList { phoneNumber };
                PositionMainForm.AcceptServrtIpList[webip] = al;
            }
            //  UpdateRichBox();
        }

        public void AsTcpCountMinus(string webip, string phoneNumber)
        {
            if (PositionMainForm.AcceptServrtIpList.Contains(webip))
            {
                ArrayList al = PositionMainForm.AcceptServrtIpList[webip] as ArrayList;
                if (al != null)
                {
                    al.Remove(phoneNumber);
                }
            }
            // UpdateRichBox();
            if (string.IsNullOrEmpty(phoneNumber))
                return;
            //--------------------在线时间计数---
            TaxiInfo taxi = PositionMainForm.TaxiList[phoneNumber] as TaxiInfo;
            if (taxi!=null && taxi.GpsCount>=6 && 
                taxi.CarType==ConstParameters.CarTypeOfTaxi)
            {
                _bTaxiOnlineTime.Add(phoneNumber, DateTime.Now, taxi.GpsCount);
            }

            if (taxi != null )
            {
                PositionMainForm.TaxiList.Remove(phoneNumber);
            }
            LogService.WriteDebugLog(phoneNumber + "出租车在内存中移除。-----------------", DateTime.Now);
        }

        public void AcceptServerlogout(string webip)
        {
            if (webip == null) return;

            if (PositionMainForm.AcceptServrtIpList.Contains(webip))
            {
                ArrayList al = PositionMainForm.AcceptServrtIpList[webip] as ArrayList;
                if (al != null)
                {
                    foreach (string phoneNumber in al)
                    {
                        //--------------------在线时间计数-----------
                        TaxiInfo taxi = PositionMainForm.TaxiList[phoneNumber] as TaxiInfo;
                        if (taxi != null && taxi.GpsCount >= 6 &&
                                 taxi.CarType == ConstParameters.CarTypeOfTaxi)
                        {
                            _bTaxiOnlineTime.Add(phoneNumber, DateTime.Now, taxi.GpsCount);
                        }
                        if (taxi!=null)
                        {
                            PositionMainForm.TaxiList.Remove(phoneNumber);    
                        }                        
                        LogService.WriteDebugLog(phoneNumber + "出租车在内存中移除。-----------------", DateTime.Now);

                    }
                }
                PositionMainForm.AcceptServrtIpList.Remove(webip);
                //UpdateRichBox();
            }
        }

        public bool TaxiOnlineTimeAdd(string phone,DateTime date,int minutes)
        {
            return _bTaxiOnlineTime.Add(phone, date, minutes);
        }

        public void ClearTaxiOnlineTime(object isTodaysCount)
        {
            //标记是当天的记录还是前一天的记录
            bool isToday = (bool)isTodaysCount;
            lock (PositionMainForm.TaxiList.SyncRoot)
            {
                foreach (TaxiInfo taxi in PositionMainForm.TaxiList.Values)
                {
                    if ( taxi.CarType==ConstParameters.CarTypeOfTaxi
                         && taxi.GpsCount>0 )
                    {
                        if (isToday)
                        {
                            _bTaxiOnlineTime.Add(taxi.DriverPhoneNumber,
                                                 DateTime.Now, taxi.GpsCount);
                        }
                        else
                        {
                            _bTaxiOnlineTime.Add(taxi.DriverPhoneNumber,
                                DateTime.Now.AddDays(-1), taxi.GpsCount);
                        }
                        taxi.GpsCount = 0;
                    }
                }
            } 
        }

        public string GetFreeAcceptServerIp(string webip)
        {
            return webip;
            string webIptemp = webip;
            int minCount = int.MaxValue;

            lock (PositionMainForm.AcceptServrtIpList.SyncRoot)
            {
                foreach (DictionaryEntry dictionaryEntry in PositionMainForm.AcceptServrtIpList)
                {
                    int tempCount = ((ArrayList)dictionaryEntry.Value).Count;

                    if (webip == dictionaryEntry.Key.ToString())
                    {
                        tempCount--;
                    }
                    if (tempCount < minCount)
                    {
                        minCount = tempCount;
                        webIptemp = dictionaryEntry.Key.ToString();
                    }
                }
            }

            return webIptemp;
        }

        public DataSet TaxiGetAdSeting(string phoneNumber)
        {
            TaxiInfo taxiInfoCache = PositionMainForm.TaxiList[phoneNumber] as TaxiInfo;
            if (taxiInfoCache != null)
            {
                return _bTaxiAd.GetList("TaxiId=" + taxiInfoCache.Id);
            }
            return null;
        }

        public DataSet TaxiGetAreaSeting(string phoneNumber)
        {
            TaxiInfo taxiInfoCache = PositionMainForm.TaxiList[phoneNumber] as TaxiInfo;
            if (taxiInfoCache != null)
            {
                return _bTaxiArea.GetList("TaxiId=" + taxiInfoCache.Id);
            }
            return null;
        }


        //delegate void AppendMessageDelegate(string msg);
        //public void AppendMessage(string msg)
        //{
        //    if (PositionMainForm.MForm.InvokeRequired)
        //    {
        //        AppendMessageDelegate d = AppendMessage;
        //        PositionMainForm.MForm.Invoke(d, new object[] { msg });
        //    }
        //    else
        //    {
        //        PositionMainForm.Rb.Text = msg;
        //        PositionMainForm.Tb.SelectedIndex = 1;
        //        PositionMainForm.Rb.ScrollToCaret();
        //        PositionMainForm.Rb.Select(PositionMainForm.Rb.Lines.Length, PositionMainForm.Rb.SelectionLength);
        //    }
        //}
        //private void UpdateRichBox()
        //{
        //    string temp = string.Empty;
        //    lock (PositionMainForm.AcceptServrtIpList.SyncRoot)
        //    {
        //        foreach (DictionaryEntry dictionaryEntry in PositionMainForm.AcceptServrtIpList)
        //        {
        //            temp += string.Format("接入服务端域名（IP）：{0}，当前客户端连接数：{1}{2}",
        //              dictionaryEntry.Key, ((ArrayList)dictionaryEntry.Value).Count, Environment.NewLine);
        //        }
        //    }
        //    AppendMessage(temp);
        //}





        public List<TaxiInfo> GetNearbyGps(double longitude, double latitude, double distance, int type)
        {
            double d = distance / 100.0f;
            double d2 = d;

            lock (PositionMainForm.TaxiList.SyncRoot)
            {

                List<TaxiInfo> taxiInfos = new List<TaxiInfo>();

                if (type == ConstParameters.TypePhoneUser)
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d
                            && Math.Abs(taxiInfo.Longitude - longitude) < d2
                             && (taxiInfo.TaxiState == 0
                            || taxiInfo.TaxiState == 20 || taxiInfo.TaxiState == 21
                            || taxiInfo.TaxiState == 22 || taxiInfo.TaxiState == 23)
                            && taxiInfo.CarType == 1//****
                             // && taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                            ));
                }
                //if (type == ConstParameters.TypePhoneUser)
                //{
                //    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                //        Where(taxiInfo =>CommonRoutine.GetDistanceBetweenTwoGps(taxiInfo.Latitude,
                //            taxiInfo.Longitude,latitude,longitude) <=distance*1000                             
                //            && taxiInfo.TaxiState == 0
                //            && taxiInfo.CarType == 1//****
                //            ));
                //}
                else if (type == ConstParameters.TypeTaxi)
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d
                            && Math.Abs(taxiInfo.Longitude - longitude) < d2
                             && taxiInfo.CarType == 1//****
                             //  && taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                            ));
                }
                else
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                     Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d2
                       && Math.Abs(taxiInfo.Longitude - longitude) < d
                        &&taxiInfo.CarType==1//****
                       // && taxiInfo.UpdateTime>DateTime.Now.AddMinutes(-10)
                       ));
                }
              
                return taxiInfos;
            }
            #region Test Method 2
            //List<TaxiInfo> taxiInfos = new List<TaxiInfo>();
            //foreach (TaxiInfo taxiInfo in MainForm.TaxiList.Values)
            //{
            //    //if (Common.CommonRoutine.GetShortDistance(longitude, latitude, taxiInfo.Longitude, taxiInfo.Latitude) <= distance)
            //    //{
            //    //    taxiInfos.Add(taxiInfo);
            //    //}
            //    if (Math.Abs(taxiInfo.Latitude - latitude) < distance && Math.Abs(taxiInfo.Longitude - longitude) < distance)
            //    {
            //        taxiInfos.Add(taxiInfo);
            //    }
            //}
            #endregion
        }

        public List<TaxiInfo> GetNearbyGpsByCarType(string phoneNumber,
            double longitude, double latitude,
            double distance, int usertype,byte carType)
        {
            double d = distance / 100.0f;
            double d2 = d;

            lock (PositionMainForm.TaxiList.SyncRoot)
            {

                List<TaxiInfo> taxiInfos = new List<TaxiInfo>();
               
                //如果为救护车  乘客不能搜索到车 或只能搜索到指定的车
                if (carType==ConstParameters.CarTypeOfAmbulance)
                {
                    PassengerInfo psg = PositionMainForm.PassengerList[phoneNumber] as PassengerInfo;
                    //该乘客已存在 呼叫救护车 且已指定了救护车
                    if (psg != null && 
                        psg.Type == ConstParameters.CarTypeOfAmbulance
                        && !string.IsNullOrEmpty(psg.CarPhoneNumber))
                    {

                       taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                       Where(taxiInfo =>  
                            taxiInfo.CarType == carType
                            && taxiInfo.DriverPhoneNumber == psg.CarPhoneNumber
                         ));
                        return taxiInfos;
                    }
                    else // 该乘客尚未打电话叫车 或尚未指定救护车
                    {
                        return taxiInfos;
                    }

                }
               

                if (usertype == ConstParameters.TypePhoneUser)
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d
                            && Math.Abs(taxiInfo.Longitude - longitude) < d2
                            && (taxiInfo.TaxiState == 0
                            || taxiInfo.TaxiState == 20 || taxiInfo.TaxiState == 21
                            || taxiInfo.TaxiState == 22 || taxiInfo.TaxiState == 23)
                            && taxiInfo.CarType == carType
                          //  && taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                          ));
                }
                
                else if (usertype == ConstParameters.TypeTaxi)
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d
                            && Math.Abs(taxiInfo.Longitude - longitude) < d2
                            && taxiInfo.CarType == carType
                          //  && taxiInfo.TaxiState <10
                              //&& taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                            ));
                }
                else
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                     Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d2
                       && Math.Abs(taxiInfo.Longitude - longitude) < d
                     //  && taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)                 
                       ));
                }

                return taxiInfos;
            }
        }

        public List<TaxiInfo> GetNearbyGpsWithStateByCarType(string phoneNumber, 
            double longitude, double latitude, double distance, int usertype, byte carType)
        {
            double d = distance / 100.0f;
            double d2 = d;

            lock (PositionMainForm.TaxiList.SyncRoot)
            {

                List<TaxiInfo> taxiInfos = new List<TaxiInfo>();

                //如果为救护车  乘客不能搜索到车 或只能搜索到指定的车
                if (carType == ConstParameters.CarTypeOfAmbulance)
                {
                    PassengerInfo psg = PositionMainForm.PassengerList[phoneNumber] as PassengerInfo;
                    //该乘客已存在 呼叫救护车 且已指定了救护车
                    if (psg != null &&
                        psg.Type == ConstParameters.CarTypeOfAmbulance
                        && !string.IsNullOrEmpty(psg.CarPhoneNumber))
                    {

                        taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo =>
                             taxiInfo.CarType == carType
                             && taxiInfo.DriverPhoneNumber == psg.CarPhoneNumber
                          ));
                        return taxiInfos;
                    }
                    else // 该乘客尚未打电话叫车 或尚未指定救护车
                    {
                        return taxiInfos;
                    }

                }
               



                if (usertype == ConstParameters.TypePhoneUser)
                {
                     
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d
                            && Math.Abs(taxiInfo.Longitude - longitude) < d2
                            && (taxiInfo.TaxiState == 0
                            || taxiInfo.TaxiState == 20 || taxiInfo.TaxiState == 21
                            || taxiInfo.TaxiState == 22 || taxiInfo.TaxiState == 23)
                            && taxiInfo.CarType == carType
                        //  && taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                          ));
                }

                else if (usertype == ConstParameters.TypeTaxi)
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d
                            && Math.Abs(taxiInfo.Longitude - longitude) < d2
                            && taxiInfo.CarType == carType
                        //&& taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                            ));
                }
                else
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                     Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d2
                       && Math.Abs(taxiInfo.Longitude - longitude) < d
                        //  && taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)                 
                       ));
                }

                return taxiInfos;
            }
        }


        public List<PassengerInfo> GetNearbyPassengerByCarType(
            double longitude, double latitude, double distance, 
            int usertype, string carType)
        {
            double d = distance / 100.0f;
            double d2 = d;

            lock (PositionMainForm.PassengerList.SyncRoot)
            {
                List<PassengerInfo> psgInfos = new List<PassengerInfo>();
                psgInfos.AddRange(PositionMainForm.PassengerList.Values.Cast<PassengerInfo>().
                     Where(psgInfo => Math.Abs(psgInfo.Lat - latitude) < d2
                       && Math.Abs(psgInfo.Lng - longitude) < d
                       && carType.Contains("|"+psgInfo.Type+"|")
                       ));                 
                return psgInfos;
            }
        }

        public List<PassengerInfo> GetAllPassengerByCarType(int usertype, string carType)
        {
          
            lock (PositionMainForm.PassengerList.SyncRoot)
            {
                List<PassengerInfo> psgInfos = new List<PassengerInfo>();
                psgInfos.AddRange(PositionMainForm.PassengerList.Values.Cast<PassengerInfo>().
                     Where(psgInfo => carType.Contains("|" + psgInfo.Type + "|")
                       ));
                return psgInfos;
            }
        }



        public List<TaxiInfo> GetNearbyGpsByCompany(double longitude, double latitude, double distance, int type,string company)
        {
            double d = distance  / 100.0f;
            double d2 = d;

            lock (PositionMainForm.TaxiList.SyncRoot)
            {

                List<TaxiInfo> taxiInfos = new List<TaxiInfo>();

                if (type == ConstParameters.TypePhoneUser)
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d
                            && Math.Abs(taxiInfo.Longitude - longitude) < d2
                            && taxiInfo.TaxiState == 0
                            //&& taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                            ));
                }
                else if (type == ConstParameters.TypeTaxi)
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                        Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d
                            && Math.Abs(taxiInfo.Longitude - longitude) < d2 
                            &&taxiInfo.CompanyName==company
                            //&& taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                            ));
                }
                else
                {
                    taxiInfos.AddRange(PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().
                     Where(taxiInfo => Math.Abs(taxiInfo.Latitude - latitude) < d2
                       && Math.Abs(taxiInfo.Longitude - longitude) < d
                       && taxiInfo.CompanyName != null 
                       && taxiInfo.CompanyName.StartsWith(company) 
                         && taxiInfo.CarType == 1//****
                        // && taxiInfo.UpdateTime > DateTime.Now.AddMinutes(-10)
                       ));
                }

                return taxiInfos;
            }          
        }

        public ArrayList ReadSms()
        {
            lock (PositionMainForm.SmsList.SyncRoot)
            {
                ArrayList al = null;
                object phoneNo = null;
                foreach (DictionaryEntry dictionary in PositionMainForm.SmsList)
                {
                    al = (ArrayList)dictionary.Value;
                    phoneNo = dictionary.Key;
                    break;
                }
                if (phoneNo != null)
                {
                    PositionMainForm.SmsList.Remove(phoneNo);
                }
                return al;
            }
        }

        public int SendSms(string phone, string content)
        {
            if (PositionMainForm.IsUseSmsModel)
            {
                return PositionMainForm.SMSSendMessage(content, phone);
            }
            else
            {
                return -1;
            }
        }

        public bool SmsQuery(int id)
        {
            if (PositionMainForm.IsUseSmsModel)
            {
                if (PositionMainForm.SMSQuery(id) == 1)
                {
                    return true;
                }
                return false;
            }
            return false;
        }

        /// <summary>
        /// Add Trade successful  Info
        /// </summary>
        /// <param name="tradeInfo"></param>
        public void AddTradeInfo(TradeInfo tradeInfo)
        {
            _bTrade.Add(tradeInfo);
        }

        public DataSet TradeGetReport(string strWhere)
        {
            return _bTrade.GetReport(strWhere);
        }

        /// <summary>
        /// Verify client platform user name and password 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckUser(string userName, string password)
        {
            bool bCheckUser = _bUser.CheckUser(userName, password);
            Console.WriteLine(DateTime.Now.ToString() + "bCheckUser--" + bCheckUser.ToString());
            return bCheckUser;

        }

        public bool ChangePassWordByUserName(string userName, string newPwd)
        {
            return _bUser.ChangePassWordByUserName(userName, newPwd);
        }

        public List<Model.TaxiInfo> GetAllTaxiInfos()
        {
            // Console.WriteLine("GetAllTaxiInfos_______1");
            lock (PositionMainForm.TaxiList.SyncRoot)
            {

                //List<TaxiInfo> list = PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().Where(
                    //taxiInfo=>taxiInfo.UpdateTime>DateTime.Now.AddMinutes(-10)).ToList();

                List<TaxiInfo> list = PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().ToList();

                //  Console.WriteLine("GetAllTaxiInfos_______2");
                return list;

            }
        }

        public List<Model.TaxiInfo> GetAllTaxiInfosByCompany(string company)
        {            
            lock (PositionMainForm.TaxiList.SyncRoot)
            {               
                //return PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().ToList().Where(
                //    taxi => taxi.CompanyName == company
                //    && taxi.UpdateTime > DateTime.Now.AddMinutes(-10)).ToList();
               List<TaxiInfo>   sd =  PositionMainForm.TaxiList.Values.Cast<TaxiInfo>().ToList().Where(
                  taxi => taxi.CompanyName!=null &&
                  taxi.CompanyName.StartsWith(company)).ToList();
               Console.WriteLine(sd.Count + "   " + company);
               return sd;
                
            }
        }

        public bool TaxiInfoAdd(TaxiInfo model)
        {
            return _bTaxi.Add(model);
        }

        public List<string> GetCompanyList()
        {
            return _bCompany.GetCompanyList();
        }

        public TaxiInfo TaxiGetModel(int id)
        {
            return _bTaxi.GetModel(id);
        }

        public bool TaxiInfoUpdate(TaxiInfo model)
        {
            return _bTaxi.Update(model);
        }

        public bool TaxiInfoDelete(int id)
        {
            return _bTaxi.Delete(id);
        }

        public bool TaxiInfoDeleteList(string idlist)
        {

            return _bTaxi.DeleteList(idlist);
        }

        public DataSet TaxiInfoGetList(string strWhere)
        {
            return _bTaxi.GetList(strWhere);
        }

        public DataSet TaxiInfoGetListWithPar(string parameter, string parameterVaule)
        {
            return _bTaxi.GetList(parameter, parameterVaule);
        }

        public bool TaxiInfoisExist(string taxiNo)
        {
            return _bTaxi.Exists(taxiNo);
        }

        public bool DriverAdd(DriverInfo driverInfo)
        {
            return _bDriver.Add(driverInfo);
        }
        public bool DriverExistphone(string driverPhoneNum)
        {
            return _bDriver.ExistsPhoneNumber(driverPhoneNum);
        }

        public bool DriverUpdate(DriverInfo model)
        {
            return _bDriver.Update(model);
        }

        public DriverInfo DriverGetModel(int id)
        {
            return _bDriver.GetModel(id);
        }

        public bool DriverDelete(int id)
        {
            return _bDriver.Delete(id);
        }

        public bool DriverDeleteList(string idlist)
        {
            return _bDriver.DeleteList(idlist);
        }

        public DataSet DriverGetList(string strWhere)
        {
            return _bDriver.GetList(strWhere);
        }

        public DataSet DriverGetListWithPar(string parameter, string parameterVaule)
        {
            return _bDriver.GetList(parameter, parameterVaule);
        }

        public bool DriverExist(string wrokId)
        {
            return _bDriver.Exists(wrokId);
        }

        public bool UserAdd(UserInfo model)
        {
            return _bUser.Add(model);
        }

        public bool UserUpdate(UserInfo model)
        {
            return _bUser.Update(model);
        }

        public bool UserDelete(int id)
        {
            return _bUser.Delete(id);
        }

        public bool UserDeleteList(string idlist)
        {
            return _bUser.DeleteList(idlist);
        }

        public DataSet UserGetList(string strWhere)
        {
            return _bUser.GetList(strWhere);
        }

        public bool UserExist(string userName)
        {
            return _bUser.Exists(userName);
        }

        public bool RoleAdd(RoleInfo model)
        {
            return _bRole.Add(model);
        }

        public bool RoleUpdate(int id, string name, string permission, string remark)
        {
            return _bRole.Update(id, name, permission, remark);
        }

        public string[] GetRoleInfoByUserName(string username)
        {
            return _bRole.GetRoleInfoByUserName(username);
        }

        public bool RoleDelete(int id)
        {
            return _bRole.Delete(id);
        }

        public bool RoleDeleteList(string idlist)
        {
            return _bRole.DeleteList(idlist);
        }

        public DataSet RoleGetList(string strWhere)
        {
            return _bRole.GetList(strWhere);
        }

        public bool RoleExist(string roleName)
        {
            return _bRole.Exists(roleName);
        }

        public bool ComplaintAdd(ComplaintInfo model)
        {
            return _bComplaint.Add(model);
        }

        public bool ComplaintUpdate(ComplaintInfo model)
        {
            return _bComplaint.Update(model);
        }

        public bool ComplaintDelete(int id)
        {
            return _bComplaint.Delete(id);
        }

        public bool ComplaintDeleteList(string idlist)
        {
            return _bComplaint.DeleteList(idlist);
        }

        public DataSet ComplaintGetList(string strWhere)
        {
            return _bComplaint.GetList(strWhere);
        }

        public DataSet ComplaintGetListOfPassenger(string strWhere)
        {
            return _bComplaint.GetListOfPasseager(strWhere);
        }
        public int GetRecordCountOfUserInThisMonth(string phoneNumber, int complaintObject)
        {
            return _bComplaint.GetRecordCountOfUserInThisMonth(phoneNumber, complaintObject);
        }

        public DataSet TradeGetList(string strWhere)
        {
            return _bTrade.GetList(strWhere);
        }

        public bool TaxiAdAdd(TaxiAdInfo model)
        {
            return _bTaxiAd.Add(model);
        }

        public bool TaxiAdUpdate(TaxiAdInfo model)
        {
            return _bTaxiAd.Update(model);
        }

        public bool TaxiAdDelete(int id)
        {
            return _bTaxiAd.Delete(id);
        }

        public bool TaxiAdDeletebyTaxiIdAdId(int taxiId, int adid)
        {
            return _bTaxiAd.Delete(taxiId, adid);
        }

        public bool TaxiAdDeleteList(string idlist)
        {
            return _bTaxiAd.DeleteList(idlist);
        }

        public DataSet TaxiAdGetList(string strWhere)
        {
            return _bTaxiAd.GetList(strWhere);
        }

        public bool TaxiAdExist(int taxiId, int adId)
        {
            return _bTaxiAd.Exists(taxiId, adId);
        }

        public bool TaxiGroupAdd(TaxiGroupInfo model)
        {
            return _bTaxiGroup.Add(model);
        }
        public bool TaxiGroupExist(string taxiGroup)
        {
            return _bTaxiGroup.Exists(taxiGroup);
        }

        public bool TaxiGroupUpdate(TaxiGroupInfo model)
        {
            return _bTaxiGroup.Update(model);
        }

        public bool TaxiGroupDelete(int id)
        {
            return _bTaxiGroup.Delete(id);
        }

        public DataSet TaxiGroupGetList(string strWhere)
        {
            return _bTaxiGroup.GetList(strWhere);
        }

        public bool TaxiAreaExit(int taxiid)
        {
            return _bTaxiArea.Exists(taxiid);
        }

        public bool TaxiAreaAdd(TaxiAreaInfo model)
        {
            return _bTaxiArea.Add(model);
        }

        public bool TaxiAreaUpdate(TaxiAreaInfo model)
        {
            return _bTaxiArea.Update(model);
        }

        public bool TaxiAreaDelete(int taxiid)
        {
            return _bTaxiArea.Delete(taxiid);
        }

        public DataSet TaxiAreaGetList(string strWhere)
        {
            return _bTaxiArea.GetList(strWhere);
        }


        public bool GmapoverlayAdd(Model.GmapoverlayInfo model)
        {
            return _bGmapoverlay.Add(model);
        }

        public bool GmapoverlayUpdate(Model.GmapoverlayInfo model)
        {
            return _bGmapoverlay.Update(model);
        }

        public bool GmapoverlayDelete(int id)
        {
            return _bGmapoverlay.Delete(id);
        }

        public Model.GmapoverlayInfo GmapoverlayGetModel(int id)
        {
            return _bGmapoverlay.GetModel(id);
        }

        public DataSet GmapoverlayGetList(string strWhere)
        {
            return _bGmapoverlay.GetList(strWhere);
        }

        public DataSet GmapoverlayGetListByParameter(string parameter, string parameterVaule)
        {
            return _bGmapoverlay.GetList(parameter, parameterVaule);
        }

        public int GmapoverlayGetRecordCount(string strWhere)
        {
            return _bGmapoverlay.GetRecordCount(strWhere);
        }

        public DataSet PhoneUserGetList(string strWhere)
        {
            return _bPhoneuser.GetList(strWhere);
        }

        public DataSet PhoneUserGetReportByTime(int dateType, string strWhere)
        {
            return _bPhoneuser.GetReportByTime(dateType, strWhere);
        }

        public DataSet PhoneGetReportByLoginCount(int[] rangeValue, string strWhere)
        {
            return _bPhoneuser.GetReportByLoginCount(rangeValue, strWhere);
        }

        public DataSet PhoneUserGetReportByTradeTimes(int[] rangeValue, string strWhere)
        {
            return _bPhoneuser.GetReportByTradeTimes(rangeValue, strWhere);
        }

        public DataSet PhoneUserGetReportByRecommender(int[] rangeValue, string strWhere)
        {
            return _bPhoneuser.GetReportByRecommender(rangeValue, strWhere);
        }

        public int PhoneUserGetCount(string parameter, string parameterValue)
        {
            return _bPhoneuser.GetCount(parameter,parameterValue);
        }

        public DataSet PhoneUserGetListByPage(int page, int pagesize, string parameter, string parameterValue)
        {
            return _bPhoneuser.GetListByPage(page, pagesize,parameter,parameterValue);
        }



        public bool CallRecordsAdd(Model.CallRecordsInfo callRecordsInfo)
        {
            return _bCallRecords.Add(callRecordsInfo);
        }

        public DataSet CallRecordsGetList(string strWhere)
        {
            return _bCallRecords.GetList(strWhere);
        }

        public int CallRecordsGetCount(string strWhere)
        {
            return _bCallRecords.GetCount(strWhere);
        }

        public DataSet CallRecordsGetListByPage(int page, int pagesize, string strWhere)
        {
            return _bCallRecords.GetListByPage(page, pagesize, strWhere);
        }

        public DataSet CallRecordsGetReportByDay(string strWhere)
        {
            return _bCallRecords.GetReportByDay(strWhere);
        }

        public bool TaxiWarmingUpdate(Model.TaxiWarmingInfo model)
        {
            return _bTaxiWarming.Update(model);
        }

        public bool TaxiWarmingDelete(int id)
        {
            return _bTaxiWarming.Delete(id);
        }

        public DataSet TaxiWarmingGetList(string strWhere)
        {
            return _bTaxiWarming.GetList(strWhere);
        }

        public DataSet TaxiWarmingGetListByPage(int page, int pagesize, string strWhere)
        {
            return _bTaxiWarming.GetListByPage(page, pagesize, strWhere);
        }

        public int TaxiWarmingGetRecordCount(string strWhere)
        {
            return _bTaxiWarming.GetRecordCount(strWhere);
        }

        public string GetClientVersion()
        {
            return OperateConfig.GetClientVersion();
        }

        public bool ServerConfigExists(string servername)
        {
            return _bServerConfig.Exists(servername);
        }

        public bool ServerConfigAdd(ServerConfigInfo model)
        {
            return _bServerConfig.Add(model);
        }

        public bool ServerConfigUpdate(ServerConfigInfo model)
        {
            return _bServerConfig.Update(model);
        }

        public bool ServerConfigDelete(int id)
        {
            return _bServerConfig.Delete(id);
        }

        public ServerConfigInfo ServerConfigGetModelById(int id)
        {
            return _bServerConfig.GetModel(id);
        }

        public ServerConfigInfo ServerConfigGetModelByName(string servername)
        {
            return _bServerConfig.GetModelByName(servername);
        }

        public DataSet ServerConfigGetList(string strWhere)
        {
            return _bServerConfig.GetList(strWhere);
        }

        public ServerConfigInfo GetServerConfigByTaxiId(int id)
        {
            return _bTaxi.GetServerConfigByTaxiId(id);
        }

        public bool UpdateServerIdByTaxiId(int taxiId, int serverId)
        {
            return _bTaxi.UpdateServerIdByTaxiId(taxiId, serverId);
        }

        public List<string> GetBlackListOfPasseager()
        {
           return _bComplaint.GetBlackListOfPasseager();
        }



        public bool AdMachineExists(string number)
        {
            return _bAdMachine.Exists(number);
        }
          
        public bool AdMachineAdd(Model.ADMachineInfo model)
        {
            return _bAdMachine.Add(model);
        }
         
        public bool AdMachineUpdate(Model.ADMachineInfo model)
        {
            return _bAdMachine.Update(model);
        }
         
        public bool AdMachineDelete(string number)
        {
            return _bAdMachine.Delete(number);
        }
         
        public bool AdMachineDeleteList(string idlist)
        {
            return _bAdMachine.DeleteList(idlist);
        }
          
        public ADMachineInfo AdMachineGetModel(int id)
        {
            return _bAdMachine.GetModel(id);
        }
        
        public DataSet AdMachineGetList(string strWhere)
        {
            return _bAdMachine.GetList(strWhere);
        }

        public int AdMachineUpdateUrlInList(string adUrl, string stationlist)
        {
            return _bAdMachine.UpdateUrlInList(adUrl, stationlist);
        }





        public bool PrivateCarDeleteById(int id)
        {
            return _bPrivateCar.DeleteById(id);
        }

        public DataSet PrivateCarGetList(string strWhere)
        {
            return _bPrivateCar.GetList(strWhere);
        }

        public DataSet PrivateCarGetListByPage(int page, int pagesize, string parameter, string parameterValue)
        {
            return _bPrivateCar.GetListByPage(page,pagesize,parameter,parameterValue);
        }

        public int PrivateCarGetCount(string parameter, string parameterValue)
        {
             return _bPrivateCar.GetCount(parameter, parameterValue);
        }

        public DataSet TaxiOnlineTimeGetList(string date1, string date2, string parameter, string parameterVaule)
        {
            return _bTaxiOnlineTime.GetList(date1, date2, parameter, parameterVaule);
        }

        public DataSet TaxiOnlineTimeGetListWhere(string date1, string date2, string strWhere)
        {
              return _bTaxiOnlineTime.GetListWhere(date1, date2, strWhere);
        }

        public bool TaxiRouteHistoryAdd(TaxiRouteHistoryInfo model)
        {
            return _bTaxiRouteHistory.Add(model);
        }
       
        public DataSet TaxiRouteGetListByPhoneNumberAndTime(string phoneNumber, string startTime, string endTime)
        {
            return _bTaxiRouteHistory.GetListByPhoneNumberAndTime(phoneNumber, startTime, endTime);
        }

        public DataSet TaxiRouteGetStatisticsByPara(string phoneNumber, string startTime, string endTime)
        {
            return _bTaxiRouteHistory.GetStatisticsByPara(phoneNumber, startTime, endTime);
        }

        public DataSet TaxiRouteGetStatisReportResult(
            int intObject, string strValue,int dateType, string startTime, string endTime)
        {
            return _bTaxiRouteHistory.GetStatisReportResult(
                intObject, strValue, dateType, startTime, endTime);
        }


        public bool TaxiNoPsgConfigExists(string phoneNumber)
        {
            return _btaxiNoPsgConfig.Exists(phoneNumber);
        }

        public bool TaxiNoPsgConfigAdd(Model.TaxiNoPsgDistanceConfig model)
        {
            return _btaxiNoPsgConfig.Add(model);
        }

        public bool TaxiNoPsgConfigUpdate(Model.TaxiNoPsgDistanceConfig model)
        {
            return _btaxiNoPsgConfig.Update(model);
        }

        public bool TaxiNoPsgConfigDelete(int id)
        {
            return _btaxiNoPsgConfig.Delete(id);
        }

        public DataSet TaxiNoPsgConfigGetList(string strWhere)
        {
            return _btaxiNoPsgConfig.GetList(strWhere);
        }

        public Model.TaxiNoPsgDistanceConfig TaxiNoPsgConfigGetModel(string phoneNumber)
        {
            return _btaxiNoPsgConfig.GetModel(phoneNumber);
        }

        public bool CompanyExists(string companyName)
        {
            return _bCompany.Exists(companyName);
        }

        public bool CompanyAdd(Model.CompanyInfo model)
        {
            return _bCompany.Add(model);
        }

        public bool CompanyUpdate(Model.CompanyInfo model)
        {
            return _bCompany.Update(model);
        }

        public bool CompanyDelete(int id)
        {
            return _bCompany.Delete(id);
        }

        public Model.CompanyInfo CompanyGetModel(int id)
        {
            return _bCompany.GetModel(id);
        }

        public DataSet CompanyGetList(string strWhere)
        {
            return _bCompany.GetList(strWhere);
        }

        public Model.UserInfo CheckUserWithReturn(string userName, string password)
        {
            return _bUser.CheckUserWithReturn(userName, password);
        }
         
        public string GetUserAreaByPhoneNumber(string phonenumber)
        {
            return _bPhoneuser.GetUserAreaByPhoneNumber(phonenumber);
        }

        public DataSet PrivateGetPhoneNumberList(string strWhere) 
        {
            return _bPrivateCar.GetPhoneNumberList(strWhere);
        }

        #region  RentCar
        public bool RentCarPhoneNumberExists(string phoneNumber)
        {
            return _bRentCar.PhoneNumberExists(phoneNumber);
        }

        public bool RentCarAdd(Model.RentCarInfo model)
        {
            return _bRentCar.Add(model);
        }

        public bool RentCarUpdate(Model.RentCarInfo model)
        {
            return _bRentCar.Update(model);
        }

        public bool RentCarDelete(int id)
        {
            return _bRentCar.Delete(id);
        }

        public Model.RentCarInfo RentCarGetModel(int id)
        {
            return _bRentCar.GetModel(id);
        }

        public DataSet RentCarGetList(string strWhere)
        {
            return _bRentCar.GetList(strWhere);
        }
        #endregion

        #region CityOfRentCar

        public bool CityOfRentCarExists(string cityName)
        {
            return _bCityOfRentCar.Exists(cityName);
        }

        public bool CityOfRentCarAdd(Model.CityOfRentCarInfo model)
        {
            return _bCityOfRentCar.Add(model);
        }

        public bool CityOfRentCarUpdate(Model.CityOfRentCarInfo model)
        {
            return _bCityOfRentCar.Update(model);
        }

        public bool CityOfRentCarDelete(int id)
        {
            return _bCityOfRentCar.Delete(id);
        }

        public Model.CityOfRentCarInfo CityOfRentCarGetModel(int id)
        {
            return _bCityOfRentCar.GetModel(id);
        }

        public DataSet CityOfRentCarGetList(string strWhere)
        {
            return _bCityOfRentCar.GetList(strWhere);
        }
        #endregion

        #region SpecialCar

        public bool UpdateImeiByPhoneNumber(string phoneNumber, string imei)
        {
            return _bSpecialCar.UpdateImeiByPhoneNumber(phoneNumber, imei);
        }

        public bool SpecialCarPhoneNumberExists(string phoneNumber)
        {
            return _bSpecialCar.PhoneNumberExists(phoneNumber);
        }

        public bool SpecialCarAdd(Model.SpecialCarInfo model)
        {
            return _bSpecialCar.Add(model);
        }

        public bool SpecialCarUpdate(Model.SpecialCarInfo model)
        {
            return _bSpecialCar.Update(model);
        }

        public bool SpecialCarDelete(int id)
        {
            return _bSpecialCar.Delete(id);
        }

        public Model.SpecialCarInfo SpecialCarGetModel(int id)
        {
            return _bSpecialCar.GetModel(id);
        }

        public DataSet SpecialCarGetList(string strWhere)
        {
            return _bSpecialCar.GetList(strWhere);
        }
        #endregion

        #region PhonePlatform

        public DataSet PhonePlatformGetList(string strWhere)
        {
            return _bPhonePlatform.GetList(strWhere);
        }

        public DataSet PhonePlatformGetListWithParameter(string parameter, string parameterVaule)
        {
            return _bPhonePlatform.GetList(parameter, parameterVaule);
        }

        #endregion

        #region CardCheckHistory

        public DataSet CardCheckHistoryGetListWithParamater(string companyName,
        string phoneNumber, string startTime, string endTime)
        {
            return _bCardCheckHistory.GetListWithParamater(companyName, 
                phoneNumber, startTime, endTime);
        }
        #endregion

        #region  Destination
        public bool DestinationExists(string name)
        {
            return _bDestination.Exists(name);
        }

        public DestinationInfo DestinationAdd(Model.DestinationInfo model)
        {
            return _bDestination.Add(model);
        }

        public bool DestinationDelete(int id)
        {
            return _bDestination.Delete(id);
        }

        public Model.DestinationInfo DestinationGetModel(int id)
        {
            return _bDestination.GetModel(id);
        }

        public DestinationInfo DestinationGetModelByName(string name)
        {
            return _bDestination.GetModelByName(name);
        }

        public DataSet DestinationGetList(string strWhere)
        {
            return _bDestination.GetList(strWhere);
        }

        public List<DestinationInfo> DestinationToList()
        {
            DataSet ds = _bDestination.GetList("");
            List<DestinationInfo> list = new List<DestinationInfo>();
            if (ds != null)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    DestinationInfo model = new DestinationInfo();
                    model.Id = int.Parse(row["Id"].ToString());
                    model.Name = row["Name"].ToString();
                    model.Lat = double.Parse(row["Lat"].ToString());
                    model.Lng = double.Parse(row["Lng"].ToString());
                    model.Type = int.Parse(row["Type"].ToString());
                    list.Add(model);
                }
            }
            return list;
        }

        #endregion

        #region TAXICDMA
        public bool CdmaTaxiExistsDeviceNumber(string deviceNumber)
        {
            return _bTaxiCdma.ExistsDeviceNumber(deviceNumber);
        }

        public bool CdmaTaxiAdd(Model.TaxiCdmaInfo model)
        {
            return _bTaxiCdma.Add(model);
        }

        public bool CdmaTaxiUpdate(Model.TaxiCdmaInfo model)
        {
            return _bTaxiCdma.Update(model);
        }

        public bool CdmaTaxiDelete(int id)
        {
            return _bTaxiCdma.Delete(id);
        }

        public bool CdmaTaxiDeleteList(string idlist)
        {
            return _bTaxiCdma.DeleteList(idlist);
        }

        public Model.TaxiCdmaInfo CdmaTaxiGetModelById(int id)
        {
            return _bTaxiCdma.GetModelById(id);
        }

        public Model.TaxiCdmaInfo CdmaTaxiGetModelByDeviceNumber(string deviceNumber)
        {
            return _bTaxiCdma.GetModelByDeviceNumber(deviceNumber);
        }

        public DataSet CdmaTaxiGetList(string strWhere)
        {
            return _bTaxiCdma.GetList(strWhere);
        }
        #endregion


        private BDbBackUpToAccess _bDbBackUpToAccess = new BDbBackUpToAccess();
        public void BackUpDbFromMySqlToAccess(string path)
        {

            _bDbBackUpToAccess.BackUp(path);
        }

        public void RemoveCdmaTaxi(string phoneNumber)
        {
            if (PositionMainForm.TaxiList.Contains(phoneNumber))
            {
                PositionMainForm.TaxiList.Remove(phoneNumber);
            } 
        }

    }
}
